﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Csla.Core;
using OpenExpressApp.Editors;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Module.WPF.Editors;
using OpenExpressApp.Types;
using OpenExpressApp.WPF.Command;
using System.Windows.Forms;

namespace OpenExpressApp.Module.WPF.Command
{
    class FileAttachmentCommand
    {
        [Command(CommandNames.AddFile, ImageName = "AddFile.bmp", ToolbarType = ToolbarType.Manual, Label = "添加文件")]
        public class AddFileCommand : PropertyEditorCommand
        {
            public override void Execute(PropertyEditor editor)
            {
                System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
                if (System.Windows.Forms.DialogResult.OK == dialog.ShowDialog())
                {
                    IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                    fileData.LoadFromFile(dialog.FileName);
                }
            }
        }

        [Command(CommandNames.ClearFile, ImageName = "ClearFile.bmp", ToolbarType = ToolbarType.Manual, Label = "清除文件")]
        public class ClearFileCommand : PropertyEditorCommand
        {
            public override bool CanExecute(PropertyEditor editor)
            {
                IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                return null != fileData.Name;
            }

            public override void Execute(PropertyEditor editor)
            {
                IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                fileData.Clear();
            }
        }

        [Command(CommandNames.OpenFile, ImageName = "OpenFile.bmp", ToolbarType = ToolbarType.Manual, Label = "打开文件")]
        public class OpenFileCommand : PropertyEditorCommand
        {
            public override bool CanExecute(PropertyEditor editor)
            {
                IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                return (null != fileData) && (string.Empty != fileData.Name);
            }

            public override void Execute(PropertyEditor editor)
            {
                IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                string filePath = Path.GetTempPath() + fileData.Name;
                fileData.SaveToFile(filePath);
                Process.Start(filePath);
            }
        }

        [Command(CommandNames.FileSaveAs, ImageName = "FileSaveAs.bmp", ToolbarType = ToolbarType.Manual, Label = "文件另存为...")]
        public class FileSaveAsCommand : PropertyEditorCommand
        {
            public override bool CanExecute(PropertyEditor editor)
            {
                IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                return null != fileData.Name;
            }

            public override void Execute(PropertyEditor editor)
            {
                var dialog = new SaveFileDialog();
                if (DialogResult.OK == dialog.ShowDialog())
                {
                    IFileData fileData = (editor as FileDataPropertyEditor).FileData;
                    fileData.SaveToFile(dialog.FileName);
                }
            }
        }
    }
}